package com.eds.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.pojo.Course;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
@Document(indexName = "course")
@Setting(settingPath = "es-settings.json")
public class CourseDocument {
    @Id
    private Long courseId;

    private Long teacherId;

    @Field(type = FieldType.Text, analyzer = "ik_pinyin")
    private String title;

    @Field(type = FieldType.Text, analyzer = "ik_pinyin")
    private String details;

    @Field(type = FieldType.Integer)
    private Integer categoryId;

    // 修改为String类型存储价格
    @Field(type = FieldType.Text)
    private String price;

    @Field(type = FieldType.Keyword)
    private Integer status;

    @Field(type = FieldType.Integer)
    private Integer recommendWeight;

    @Field(type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    @Field(type = FieldType.Long)
    private Long studyPeople;

    @Field(type = FieldType.Keyword)
    private String titleImg;

    @Field(type = FieldType.Keyword)
    private String content1Img;

    @Field(type = FieldType.Keyword)
    private String content2Img;

    @Field(type = FieldType.Keyword)
    private String content3Img;

    @Field(type = FieldType.Keyword)
    private String content4Img;

    @CompletionField(analyzer = "ik_pinyin", searchAnalyzer = "ik_smart")
    private List<String> titleSuggest;

    // 修改fromCourse方法
    public static CourseDocument fromCourse(Course course, List<String> tags) {
        CourseDocument doc = new CourseDocument();
        doc.setCourseId(course.getCourseId());
        doc.setTeacherId(course.getTeacherId());
        doc.setTitle(course.getTitle());
        doc.setDetails(course.getDetails());
        doc.setCategoryId(course.getCategoryId());
        // 使用新的setter方法设置价格
        doc.setPriceAsBigDecimal(course.getPrice());
        doc.setStatus(course.getStatus());
        doc.setRecommendWeight(course.getRecommendWeight());
        doc.setCreateTime(course.getCreateTime());
        doc.setStudyPeople(course.getStudyPeople());
        doc.setTitleImg(course.getTitleImg());
        doc.setContent1Img(course.getContent1Img());
        doc.setContent2Img(course.getContent2Img());
        doc.setContent3Img(course.getContent3Img());
        doc.setContent4Img(course.getContent4Img());
        ArrayList<String> suggestions = new ArrayList<>();
        suggestions.add(course.getTitle());
        suggestions.addAll(tags);
        doc.setTitleSuggest(suggestions);

        return doc;
    }

    // 添加BigDecimal类型的getter和setter
    public BigDecimal getPriceAsBigDecimal() {
        return price != null ? new BigDecimal(price) : null;
    }

    public void setPriceAsBigDecimal(BigDecimal price) {
        this.price = price != null ? price.toString() : null;
    }
}